package com.ti.shangou.controller.pages.back.merchant;

import com.ti.shangou.controller.BaseController;
import com.ti.shangou.dao.MerchantDao;
import com.ti.shangou.enums.ApprovalEnum;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Merchant;
import com.ti.shangou.pojo.entity.User;
import com.ti.shangou.pojo.query.MerchantQuery;
import com.ti.shangou.pojo.vo.BusinessTypeVO;
import com.ti.shangou.pojo.vo.MerchantVO;
import com.ti.shangou.pojo.vo.UserVO;
import com.ti.shangou.service.impl.BusinessTypeServiceImpl;
import com.ti.shangou.service.impl.ImgCacheServiceImpl;
import com.ti.shangou.service.impl.MerchantServiceImpl;
import com.ti.shangou.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 作者：田宝山
 * 日期：2020/6/3
 */
@Controller
@RequestMapping("/pages/back/merchant")
public class MerchantController extends BaseController {
    @Resource
    UserServiceImpl userService;
    @Resource
    MerchantDao merchantDao;
    @Resource
    MerchantServiceImpl merchantService;
    @Resource
    ImgCacheServiceImpl imgCacheService;

    @Resource
    BusinessTypeServiceImpl businessTypeService;

    @RequestMapping("/addPre")
    public String addPre(Model model){
        List<BusinessTypeVO> types = businessTypeService.getTypes();
        model.addAttribute("types",types );
        return "pages/back/merchant/merchant-addPre";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Merchant merchant){
        // 断定pcd(省市区字段不是null和“”)，否则就抛异常
        Assert.notNull(merchant.getPcd(), "省市区字段不能为空！");// 抛出异常将会进入全局异常捕获
        String province = merchant.getPcd().split("-")[0];// 把省取出来
        merchant.setProvince(province);// 设置省份

        Date now = new Date();
        merchant.setCreateTime(now);
        merchant.setUpdateTime(now);
        merchant.setMaxDeliveryArea(3.0);// 默认最大配送距离三千米
        merchant.setMerchantId(System.currentTimeMillis());// 设置当前商户的id// 时间戳来设置不好
        merchant.setApprovalStatus(ApprovalEnum.审核中.toString());// 设置当前商户的id// 时间戳来设置不好

        UserVO user = (UserVO) getSession().getAttribute("user");
        merchant.setUserId(user.getUserId());
        User u = userService.selectUserById(user.getUserId()); // 这里查的应该是商户，不应该是user
        if (u==null){
            if (merchantDao.insertSelective(merchant) == 1) {

                imgCacheService.deleteImgCache(merchant);// 清除缓存图片

                return ResponseDTO.ok("申请成功");
            }else {
                return ResponseDTO.fail("申请失败");
            }
        }
        return ResponseDTO.fail("你已申请过商户，请不要重复申请");
    }

    @RequestMapping("approvalStatus")
    String approvalStatus(Model model) {// 在这里就应该判断以下当前用户是不是已经入驻了申请了
        return "pages/back/merchant/merchant-approvalStatus";
    }

    @RequestMapping("list")
    public String showList(){
        return "pages/back/merchant/merchant-list";
    }

    @RequestMapping("ajaxList")
    @ResponseBody
    public Object ajaxList(MerchantQuery query){
        return merchantService.ajaxList(query);
    }

    @ResponseBody
    @RequestMapping("editMerchant")
    public Object editMerchant(Merchant merchant){
        return merchantService.editMerchant(merchant);
    }





}
